package tech.summerly.weather.component.adapter.decoration

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Rect
import android.support.v7.widget.RecyclerView
import android.text.TextPaint
import android.view.View
import org.jetbrains.anko.forEachChildWithIndex
import org.jetbrains.anko.sp
import tech.summerly.weather.AppContext
import tech.summerly.weather.R
import tech.summerly.weather.util.LoggerLevel
import tech.summerly.weather.util.log
import tech.summerly.weather.util.string
import tech.summerly.weather.util.toIntString

class TemperatureItemDecoration(
        private val textGravity: Int = GRAVITY_TOP
) : RecyclerView.ItemDecoration() {

    companion object {
        const val GRAVITY_TOP = 0
        const val GRAVITY_BOTTOM = 1
    }

    private val paint = TextPaint()

    private val circleRadius: Int = 2

    init {
        paint.textSize = AppContext.instance.sp(12f).toFloat()
        paint.isAntiAlias = true
    }

    private val tempRect = Rect()

    private var temperatures = ArrayList<Float>()

    private var max = 0F
    private var min = 0F

    /**
     * 设置 ItemDecoration 绘制的温度趋势的数据
     *
     * @param temperatures can not be empty
     */
    fun setData(temperatures: List<Float>, max: Float = temperatures.max()!!, min: Float = temperatures.min()!!) {
        this.temperatures.clear()
        this.temperatures.addAll(temperatures)
        this.max = max
        this.min = min
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (temperatures.size < parent.adapter.itemCount) {
            log(LoggerLevel.ERROR) { "item count do not match!!" }
            return
        }
        var preX = 0
        var preY = 0
        parent.forEachChildWithIndex { index, child ->

            val position = parent.getChildAdapterPosition(child)

            val x = child.left + child.width / 2

            val frame = child.findViewById<View>(R.id.frameTrend)
            val y = getY(position, frame).toInt()

            //绘制圆圈
            paint.color = Color.RED
            c.drawOval((x - circleRadius).toFloat(), (y - circleRadius).toFloat(), (x + circleRadius).toFloat(), (y + circleRadius).toFloat(), paint)

            //绘制线
            paint.color = Color.DKGRAY
            if (index == 0 && position != 0) {
                preX = x
                preY = getY(position - 1, frame).toInt()
            }
            if (position > 0) {
                c.drawLine(preX.toFloat(), preY.toFloat(), x.toFloat(), y.toFloat(), paint)
            }

            //绘制文字
            val value = string(R.string.template_celsius, temperatures[position].toIntString())
            paint.getTextBounds(value, 0, value.length - 1, tempRect)
            if (textGravity == GRAVITY_TOP) {
                c.drawText(value, (x - tempRect.width() / 2).toFloat(), (y - tempRect.height()).toFloat(), paint)
            } else {
                c.drawText(value, (x - tempRect.width() / 2).toFloat(), (y + 2 * tempRect.height()).toFloat(), paint)
            }

            preX = x
            preY = y
        }
    }

    private fun getY(position: Int, frame: View): Float {
        val bottom = frame.bottom

        val scale = frame.height / (max - min)

        val t = temperatures[position]

        return bottom - ((t - min) * scale)
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
    }

}